package com.woniuxy.charin.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.charin.mapper.FormMapper;
import com.woniuxy.charin.model.entity.Contracts;
import com.woniuxy.charin.model.entity.Form;
import com.woniuxy.charin.model.entity.Investors;
import com.woniuxy.charin.service.ContractsService;
import com.woniuxy.charin.service.FormService;
import com.woniuxy.charin.service.InvestorsService;
import com.woniuxy.charin.util.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/forms")
@Api(tags = "待办项")
public class FormController {
    @Autowired
    private FormService formService;
    @Autowired
    private FormMapper formMapper;
    @Autowired
    private ContractsService  contractsService;
    @Autowired
    private InvestorsService investorsService;
    @Autowired
    private RabbitTemplate rabbitTemplate;   //消息队列
    @Autowired
    private JwtUtil jwtUtil;
    /**
     * getAll
     */
    @PostMapping("/getAll")
    @ApiOperation("获取登录账号的待办事项")
    public ResponseEntity<?>getAll(@RequestBody Map<String, Integer> params,@RequestHeader(required = false, name = "Authorization") String authorization){
        int pageNum = params.get("pageNum");
        int pageSize = params.get("pageSize");
        Page<Form> page = new Page<>(pageNum,pageSize);
        String userId = jwtUtil.getValue(authorization, "userId");

        formMapper.selectPage(page, Wrappers.lambdaQuery(Form.class).eq(Form::getUserId,Integer.valueOf(userId)));
        return ResponseEntity.ok(page);
    }
    @Transactional
    @GetMapping("/agree")
    @ApiOperation("同意")
    public ResponseEntity<?>agree( Integer formId){
        Contracts contracts = contractsService.getOne(Wrappers.lambdaQuery(Contracts.class).eq(Contracts::getFormId,formId));
        Form form = formService.getOne(Wrappers.lambdaQuery(Form.class).eq(Form::getFormId, contracts.getId()));
        form.setStatus("已通过");
        formService.updateById(form);
        contracts.setStatus("已通过");
        contractsService.updateById(contracts);
        Investors investors = investorsService.getById(formId);
        investors.setStatus("已通过");
        investorsService.updateById(investors);
        return ResponseEntity.ok(null);
    }
    /**
     * 提交
     */
    @PostMapping("/submit")
    @ApiOperation("提交")
    public ResponseEntity<?>submit(@RequestBody Form form){
        rabbitTemplate.convertAndSend("form.queue",form);
        return ResponseEntity.ok(null);
    }
}
